package com.cn.daimajiangxin.flink.sink;

import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcExecutionOptions;
import org.apache.flink.connector.jdbc.JdbcStatementBuilder;
import org.apache.flink.connector.jdbc.core.datastream.Jdbc;
import org.apache.flink.connector.jdbc.core.datastream.sink.JdbcSink;
import org.apache.flink.connector.jdbc.datasource.statements.SimpleJdbcQueryStatement;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Arrays;
import java.util.List;

public class JdbcSinkDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.enableCheckpointing(5000);
        List<User> userList = Arrays.asList(new User(1, "Alice", 25, "alice"),
                new User(2, "Bob", 30, "bob"),
                new User(3, "Charlie", 35, "charlie"));
        // 模拟用户数据
        DataStream<User> userStream = env.fromData(userList);

        JdbcExecutionOptions jdbcExecutionOptions = JdbcExecutionOptions.builder()
                .withBatchSize(1000)
                .withBatchIntervalMs(200)
                .withMaxRetries(5)
                .build();
        JdbcConnectionOptions connectionOptions = new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                .withUrl("jdbc:mysql://192.168.0.199:3306/test")
                .withDriverName("com.mysql.cj.jdbc.Driver")
                .withUsername("root")
                .withPassword("1qaz@WSX")
                .build();
        String insertSql = "INSERT INTO user (id, name, age, user_name) VALUES (?, ?, ?, ?)";
        JdbcStatementBuilder<User> statementBuilder = (statement, user) -> {
            statement.setInt(1, user.getId());
            statement.setString(2, user.getName());
            statement.setInt(3, user.getAge());
            statement.setString(4, user.getUserName());
        };
        // 创建JDBC Sink
        JdbcSink<User> jdbcSink = new Jdbc().<User>sinkBuilder()
                .withQueryStatement(new SimpleJdbcQueryStatement<User>(insertSql, statementBuilder))
                .withExecutionOptions(jdbcExecutionOptions)
                .buildAtLeastOnce(connectionOptions);
        // 添加Sink
        userStream.sinkTo(jdbcSink);
        env.execute("JDBC Sink Demo");
    }

    // 用户实体类
    public static class User {
        private int id;
        private String name;
        private String userName;
        private int age;

        public User(int id, String name, int age, String userName) {
            this.id = id;
            this.name = name;
            this.age = age;
            this.userName = userName;
        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public String getUserName() {
            return userName;
        }
    }
}